import { defineStore } from 'pinia';
import type { NotificationItem } from '@/types/interface';

import { noticeList, noticeTotal, readList, readListAll } from '@/api/notice/notice';
// const msgData = [
//   {
//     createDate: '2023/03/20 14:20:49',
//     id: 397836518879301,
//     isRead: false,
//     title: '访问富翁',
//   },
// ];

// type MsgDataType = typeof msgData;
export const useNotificationStore = defineStore('notification', {
  state: () => ({
    msgData: [],
    notifNumber: 0,
  }),
  getters: {},
  actions: {
    setMsgData(data: any) {
      this.msgData = data;
    },
    splitArr(e: number) {
      readList([e]).then((res) => {
        this.notifNumber = this.notifNumber - 1;
        this.msgData.forEach((element, index) => {
          if (element.id == e) {
            this.msgData.splice(index, 1);
          }
        });
      });
    },
    splitAll() {
      // readListAll().then((res) => {
      this.notifNumber = 0;

      this.msgData = [];
      // });
    },
    getList() {
      noticeTotal().then((res) => {
        this.notifNumber = res.data;
      });
      this.msgData = [];
      noticeList({
        isUnread: false,
        value: '',
        pageNo: 1,
        pageSize: 6,
      }).then((res) => {
        res.data.data.forEach((element) => {
          if (!element.isRead) {
            this.msgData.push(element);
          }
        });
      });
    },
  },
  persist: true,
});
